﻿/**
 * Created by Administrator on 2017/8/4.
 */
define(['jquery','api'],function ($,api) {
    var CustomerSearchOfDept = {
        name: 'CustomerSearchOfDept',
        template: ''
        + '<div class="company-one search-of-dept" id="department-search" v-cloak>'
        + '    <div class="concluded-top-two">'
        + '        <div class="one-input">'
        + '            <strong>客户等级：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.level" clearable >'
        + '                    <i-option v-for="item in levelOption" v-bind:value="item.Id" v-bind:key="item.LevelName">'
        + '                        {{ item.LevelName }}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="one-input">'
        + '            <strong>购买意向：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.purchasingIntention" clearable >'
        + '                    <i-option v-for="item in purchasingIntentionOption" v-bind:value="item.value" v-bind:key="item">'
        + '                        {{ item.label }}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="ascription one-input">'
        + '            <strong>客户归属：</strong>'
        + '            <strong class="state-input">'
        + '                    <Cascader v-bind:data="nodeOption" v-model="selectedNode"  filterable  change-on-select></Cascader>'
        //+ '                <i-select v-model="searchInfo.nodeId"  clearable>'
        //+ '                    <i-option v-for="item in nodeOption" :value="item.Id" :key="item.Name">{{ item.Name }}'
        //+ '                    </i-option>'
        //+ '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="source one-input">'
        + '            <strong>来源类型：</strong>'
        + '            <strong class="state-input">'
        + '                <i-select v-model="searchInfo.sourceType"  clearable>'
        + '                    <i-option v-for="item in sourceTypeOption" :value="item" :key="item">{{ item}}</i-option>'
        + '                </i-select>'
        + '            </strong>'
        + '        </div>'
        + '        <div class="c-search-box"><span>关键字：</span><input type="text" class="c-input"  v-model="searchInfo.searchKey" placeholder="输入手机、姓名、客户来源">'
        + '            <input type="button" class="c-search" value="搜索"  v-on:click="search">'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            poolType: String
        },
        data: function () {
            return {
                sortBy: "CreatedTime,false",
                searchInfo: {
                    level:"",
                    purchasingIntention: "",
                    sourceType: "",
                    searchKey: "",
                    nodeId: "",
                    sortBy: "",
                    pageIndex: 0,
                    pageSize: 0,
                    isAscending: "false"
                },
                purchasingIntentionOption: [
                    {
                        value: 'Strong',
                        label: '意向强'
                    },
                    {
                        value: 'Commonly',
                        label: '意向一般'
                    },
                    {
                        value: 'Weak',
                        label: '意向弱'
                    },
                    {
                        value: 'NoIntention',
                        label: '无意向'
                    },
                    {
                        value: 'NoVisit',
                        label: '未回访'
                    }
                ],
                sourceTypeOption: [],
                nodeOption: [],
                levelOption: [],
                selectedNode:[],
            }
        },
        computed: {
        },
        mounted: function () {
            var thisSearch = this;
            //监听showCustomerAdd
            window.bus.$on("showCustomerAdd", function (data) {
                thisSearch.showCustomerAdd = true;
            });
            api.Get_All_Big_Level()
                .done(function (data) {
                    if (data.IsSuccess) {
                        thisSearch.levelOption = data.ResultObject;
                    } else {
                        thisSearch.$Message.error(data.ErrMsg);
                    }
                })
                .fail(function () {
                    thisSearch.$Message.error('查询出错');
                });
            //if (thisSearch.poolType === "DeptPool") {
            //    $.get("/Department/GetDepartmentByCompanyId", { companyId: localStorage.companyId }, function (data) {
            //        if (data.IsSuccess) {
            //            thisSearch.nodeOption = data.ResultObject;
            //        } else {
            //            thisSearch.$Message.error(data.ErrMsg);
            //        }
            //    });
            //}
            //if (thisSearch.poolType === "CompanyPool") {
            //    $.get("/Company/GetCompanyById", { companyId: localStorage.companyId }, function (data) {
            //        if (data.IsSuccess) {
            //            thisSearch.nodeOption.push(data.ResultObject);
            //        } else {
            //            thisSearch.$Message.error(data.ErrMsg);
            //        }
            //    });
            //}

            api.GetOrganizaitonNode({ "nodeId": localStorage.companyId })
                .done(function (res) {
                    if (res.IsSuccess === true) {
                        if (thisSearch.poolType === "DeptPool") {
                            thisSearch.nodeOption = res.ResultObject.children;
                        }
                        if (thisSearch.poolType === "CompanyPool") {
                            thisSearch.nodeOption = [{ value: res.ResultObject.value, label: res.ResultObject.label }];
                        }
                    } else {
                        thisSearch.$Message.error(res.ErrMsg);
                    }
                })
                .fail(function () {
                    thisSearch.$Message.error('查询出错');
                });

            if (thisSearch.poolType === "BigCustomer") {
                api.Get_Node_Tree()
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisSearch.nodeOption = [data.ResultObject];
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            }
        },
        watch: {
            selectedNode: function (newNode) {
                newNode.length === 0 ? this.searchInfo.nodeId = "" : this.searchInfo.nodeId = newNode[newNode.length - 1];
            },
        },
        methods: {
            setSort: function () {
                var sortArr = this.sortBy.split(",");
                this.searchInfo.sortBy = sortArr[0];
                this.searchInfo.isAscending = sortArr[1];
            },
            search: function (isBack) {
                var thisSearch = this;
                thisSearch.setSort();
                api.Get_Pool_Customer_Count(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发changePage
                            window.bus.$emit("changePage", { totalRows: data.ResultObject, isBackFirstPage: isBack });//通过 bus 触发数据总数变更事件,并跳回第一页事件.
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
            searchNext: function (Id, isNext) {
                var thisSearch = this;
                var s = {};
                $.extend(true, s, thisSearch.searchInfo);
                s.customerId = Id;
                s.isAscending = isNext ? s.isAscending : s.isAscending === "false" ? "true" : "false";

                api.Get_Pool_Customer_Next(thisSearch.poolType,s)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发showCustomerDetail
                            window.bus.$emit("showCustomerDetail", { customerId: data.ResultObject.Id });
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
            exportData: function () {
                var thisSearch = this;

                api.Get_Pool_Customer_Count(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisSearch.searchInfo.pageIndex = 1;
                            thisSearch.searchInfo.pageSize = data.ResultObject;
                            api.Get_Pool_Customer_List(thisSearch.poolType,thisSearch.searchInfo)
                                .done(function (data) {
                                    if (data.IsSuccess) {
                                        //触发bindExportData
                                        window.bus.$emit("bindExportData", data.ResultObject);//通过 bus 触发数据绑定事件.
                                    } else {
                                        thisSearch.$Message.error(data.ErrMsg);
                                    }
                                })
                                .fail(function () {
                                    thisSearch.$Message.error('查询出错');
                                });
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
        },
        created: function () {
            var thisSearch = this;
            //ajax客户来源类型
            api.Get_Customer_SourceType_Option()
                .done(function (data) {
                    if (data.IsSuccess) {
                        thisSearch.sourceTypeOption = data.ResultObject;
                    } else {
                        thisSearch.$Message.error(data.ErrMsg);
                        //alert(data.ErrMsg)
                    }
                })
                .fail(function () {
                    thisSearch.$Message.error('查询出错');
                });
            //通过bus,监听数据查询事件,监听searchData
            window.bus.$on("searchData", function (dataPage) {
                thisSearch.searchInfo.pageIndex = dataPage.pageNum;
                thisSearch.searchInfo.pageSize = dataPage.pageSize;
                api.Get_Pool_Customer_List(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发bindData
                            window.bus.$emit("bindData", data.ResultObject);//通过 bus 触发数据绑定事件.
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            });
            //通过bus,监听查询事件，监听search
            window.bus.$on("search", function (isBack) {
                thisSearch.search(isBack);
            });

            //监听shiftCustomerToStaffSuccess
            window.bus.$on("shiftCustomerToStaffSuccess", function () {
                thisSearch.search(true);
            });
            //监听shiftCustomerToOtherDeptSuccess
            window.bus.$on("shiftCustomerToOtherDeptSuccess", function () {
                thisSearch.search(true);
            });
            //监听abandonCustomerSuccess
            window.bus.$on("abandonCustomerSuccess", function () {
                thisSearch.search(true);
            });
            //监听changeSort
            window.bus.$on("changeSort", function (sort) {
                thisSearch.sortBy = sort;
                thisSearch.search(true);
            });

            //监听afterExportData
            window.bus.$on("afterExportData", function () {
                thisSearch.search(true);
            });

            //监听nextCustomerDetail
            window.bus.$on("nextCustomerDetail", function (Id) {
                thisSearch.searchNext(Id, true);
            });

            //监听previousCustomerDetail
            window.bus.$on("previousCustomerDetail", function (Id) {
                thisSearch.searchNext(Id, false);
            });

            //监听checkOrSelectChange
            window.bus.$on("checkOrSelectChange", function (data) {
                thisSearch.searchInfo.searchGroup = "";
                for (var i = 0; i < data.length; i++) {
                    if (i == 0) {
                        thisSearch.searchInfo.searchGroup = data[i].value;
                    } else {
                        thisSearch.searchInfo.searchGroup = thisSearch.searchInfo.searchGroup + "," + data[i].value;
                    }
                }
            });

            //监听exportData
            window.bus.$on("exportData", function (Id) {
                thisSearch.exportData();
            });
        },
    };
    return CustomerSearchOfDept;
});
